package com.acme.bankapp.domain.account;

import com.acme.bankapp.exception.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {

	protected double balance;
	protected int ID;

	@Override
	public int hashCode() {
		return ID;
	}

	@Override
	public String toString() {
		return "Account [balance=" + balance+"]";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (balance == other.getBalance())
			return true;
		else {
			return false;
		}
	}

	@Override
	public double getBalance() {

		return balance;
	}

	@Override
	public void deposit(double x) {
		if (x > 0) {
			balance += x;
		}
	}

	AbstractAccount(double balance) {
		this.balance = balance;
	}

	@Override
	public void withdraw(double x) throws NotEnoughFundsException {
		if (x < 0) {
			throw new IllegalArgumentException();
		}
		if (x <= balance && x > 0) {
			balance -= x;
		} else {
			throw new NotEnoughFundsException(x);
		}
	}

	@Override
	public double maximumAmountToWithdraw() {
		return balance;
	}

	public double decimalValue() {
		return Math.round(balance);
	}
}
